﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <script src="../Scripts/jquery-3.3.1.js"></script>
    <script src="../layui/layui.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <style>
        .layui-inline .layui-input {
            height: 30px;
        }

        .layui-form-item .layui-btn + .layui-btn {
            margin-left: 60px;
        }
    </style>
</head>

<body style="margin: 20px 20px;">
    <div class="search">
        <div class="layui-inline">
            <input class="layui-input" name="judgeQuestionTitle" id="judgeQuestionTitle" placeholder="题目" autocomplete="off">
        </div>
        <button class="layui-btn layui-btn-normal  layui-btn-sm" id="search_btn" data-type="reload">搜索</button>
    </div>

    <div class="layui-btn-container" id="toolbarDemo" style="display: none">
        <div class="layui-form-item form-inline " style="margin-bottom: 0px;">
            <button class="layui-btn layui-btn-sm" id="add_btn" lay-event="AddData">添加</button>
        </div>

    </div>
    <!-- 表格行操作工具按钮 -->
    <div style="display: none" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </div>
    <div>
        <table class="layui-hide" id="question_set" lay-filter="question_set"></table>
    </div>
    <div class="layui-tab-item">
        <div id="pageDemo"></div>
    </div>

    <!-- 添加单选题 -->
    <div class="AddData" id="AddMutilQuestion" style="display: none; padding: 20px">
        <form class="layui-form" lay-filter="" id="AddMutilQuestionForm" style="margin-left: 40px;">

            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">题目</label>
                <div class="layui-input-block">
                    <textarea name="judge_question_title" placeholder="请输入内容" class="layui-textarea"></textarea>
                </div>
            </div>



            <div class="layui-form-item form-inline " style="margin-bottom: 0px;">
                <label class="layui-form-label" style="width: 100px;">正确答案</label>
                <div class="layui-input-inline">
                    <input type="radio" name="correct_answer_1" id="Radio1" title="对">
                    <input type="radio" name="correct_answer_1" id="Radio2" title="错">
                </div>
                <!-- 记录所选答案 -->
                <label class="layui-form-label">所属课程号</label>
                <div class="layui-input-inline">
                    <input type="text" name="course_id" autocomplete="off" placeholder="请输入课程号" class="layui-input">
                </div>

            </div>
        </form>
    </div>
    <!-- 编辑单选题 -->
    <div class="AddData" id="UpdateMutilQuestion" style="display: none; padding: 20px">
        <form class="layui-form" lay-filter="" id="UpdateMutilQuestionForm" style="margin-left: 40px;">
            <div class="layui-form-item layui-form-text" style="display: none;">
                <label class="layui-form-label">题号</label>
                <div class="layui-input-block">
                    <textarea name="judge_question_id" placeholder="请输入内容" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">题目</label>
                <div class="layui-input-block">
                    <textarea name="judge_question_title" placeholder="请输入内容" class="layui-textarea"></textarea>
                </div>
            </div>
             <div class="layui-form-item form-inline " style="margin-bottom: 0px;">
                <label class="layui-form-label" style="width: 100px;">正确答案</label>
                <div class="layui-input-inline ">
                    <input type="radio"  name="correct_answer" id="option_true" title="对">
                    <input type="radio"  name="correct_answer" id="option_false" title="错">
                </div>
                <!-- 记录所选答案 -->
                <label class="layui-form-label">所属课程号</label>
                <div class="layui-input-inline">
                    <input type="text" name="course_id" autocomplete="off" placeholder="请输入课程号" class="layui-input">
                </div>

            </div>
        </form>
    </div>


    <script>
        layui.use(['table', 'laypage', 'layer', 'form', 'upload'], function ()
        {
            var table = layui.table,
                laypage = layui.laypage,
                form = layui.form;

            //初始化学院
            var collegeSet;
            var collegeObj = {};
            var ins1 = table.render({ //相当于ajax
                elem: '#question_set'
                , url: 'judgeQuestionInfoManage.aspx'
                , id: 'question_set1' //重载时使用
                , title: '判断题基本信息表'
                , height: 520
                , page: true
                , data: { 'table': 'tb_judges_questions' }
                , limits: [3, 5, 10]  //一页选择显示3,5或10条数据
                , limit: 10  //一页显示10条数据
                , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
               , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter', 'print', {
                    title: '打印'
                  , layEvent: 'export'
                  , icon: 'layui-icon-export'
                }]
                , cols: [[
                    { field: 'judge_question_id', width: 120, align: 'center', title: '题目ID' }
                    , { field: 'question_type', width: 200, align: 'center', title: '题目类型' }
                    , { field: 'judge_question_title', width: 100, title: '题目' }
                          , { field: 'correct_answer', width: 250, align: 'center', title: '正确答案(对1，错误0）' }
                          , { field: 'course_id', width: 120, align: 'center', title: '所属课程号' }
                    , { fixed: 'right', width: 165, align: 'center', title: '操作', toolbar: '#barDemo' }//添加列工具条
                ]]
                , parseData: function (res)
                { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                    //console.log(JSON.stringify(res));
                    //console.log(JSON.stringify(res.code));
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.count, //解析数据长度
                        "data": res.data //解析数据列表
                    };
                }
                , error: function (xhr, state, errorThrown)
                {
                    requesFail(xhr);
                }
            })
            //监听行工具事件
            table.on('tool(question_set)', function (obj)
            { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值



                if (layEvent === 'del')
                {
                    layer.confirm('真的删除行么', function (index)
                    {
                        layer.close(index);
                       
                        //向服务端发送删除指令
                        delquestionInfo(data);
                    });
                } else if (layEvent === 'edit')
                {


                    layer.open({
                        type: 1,
                        title: '编辑题目',
                        skin: 'layui-layer-rim', //加上边框
                        area: ['750px', '300px'], //宽高
                        content: $("#UpdateMutilQuestion"),
                        btn: ['保存更改'],
                        success: function (data)
                        {
                            getData();//填充数据

                        }
                        , yes: function ()
                        {
                            updateUserInfo();
                        }
                    });
                }
                //对表单数据赋值
                function getData()
                {
                    var flag = 0;
                    
                    var items = $("#UpdateMutilQuestionForm")[0];
                    for (var i = 0; i < items.length; i++)
                    {
                        var item = items[i];
                        if (item.name != "")
                        {
                            var key = item.name;
                            var value = data[key];
                            if (key == "correct_answer" && flag == 0)
                            {
                                flag = 1;
                                var length = value.length;
                                
                                form.render();

                            }
                            else
                            {
                                item.value = value;
                            }

                        }
                    }
                }
                function delquestionInfo(data)
                {
                    $.ajax({
                        url: '../operate/operateQuestionInfo.aspx',
                        datatype: 'text',
                        data: { 'operate': layEvent, 'table': 'tb_judge_questions', 'judge_question_id': data.judge_question_id },
                        success: function (res)
                        {
                            if (res > 0)
                            {
                                obj.del(); //删除对应行（tr）的DOM结构
                                layer.msg('删除成功');
                                $(".layui-laypage-btn").click();//刷新当前分页数据
                            }
                            else
                            {
                                layer.msg('删除失败，不存在该用户');
                            }
                        },
                        error: function (xhr, textStatus, errorThrown)
                        {
                            alert("状态码：" + xhr.status + "状态:" + xhr.readyState + "错误信息:" + xhr.statusText + "请求状态：" + textStatus + errorThrown);
                            //当前状态,0-未初始化，1-正在载入，2-已经载入，3-数据进行交互，4-完成。
                        }
                    });
                }
            });
            function updatequestionInfo(para)
            {
                $.ajax({
                    url: '../operate/operateQuestionInfo.aspx',
                    datatype: 'text',
                    data: para,
                    success: function (res)
                    {
                        if (res > 0)
                        {
                            layer.alert('保存成功', { icon: 1 });
                            $(".layui-laypage-btn").click();//刷新当前分页数据
                        }
                        else
                        {
                            layer.msg('更改失败，数据不对');
                        }
                    },
                    error: function (xhr, textStatus, errorThrown)
                    {
                        alert("状态码：" + xhr.status + "状态:" + xhr.readyState + "错误信息:" + xhr.statusText + "请求状态：" + textStatus + errorThrown);
                        //当前状态,0-未初始化，1-正在载入，2-已经载入，3-数据进行交互，4-完成。
                    }
                });
            }
            
            //头工具栏事件
            table.on('toolbar(question_set)', function (obj)
            {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event)
                {
                    case 'AddData':
                        layer.open({
                            type: 1,
                            title: '添加题目',
                            skin: 'layui-layer-rim', //加上边框
                            area: ['750px', '350px'], //宽高
                            content: $("#AddMutilQuestion"),
                            btn: ['保存']
                        , yes: function (index, layero)
                        {
                            AddMutilQuestion();
                            //return false;
                        }
                        });
                        break;
                        //自定义头工具栏右侧图标 - 导出
                    case 'export':
                        layer.confirm('是否导出数据！', {
                            btn: ['确定', '取消'] //可以无限个按钮
                            , btn2: function (index, layero)
                            {

                                return true;//按钮【按钮二】的回调
                            }
                        }, function (index)
                        {

                            layer.close(index);//按钮【按钮一】的回调
                            exportFile();
                        });
                        break;
                };
            });
            function exportFile()
            {
                var judgeQuestionTitle = $('#judgeQuestionTitle').val();
                var para = {};
                para.judge_question_title = judgeQuestionTitle;
                $.ajax({
                    url: '../export/judgeExport.aspx',
                    methods: 'post',
                    dataType: 'json',
                    data: para,
                    success: function (res)
                    {
                        table.exportFile(ins1.config.id, res.data);
                    },
                    error: function (res)
                    {
                        alert("导出失败");
                    }
                });

            }
            $('#search_btn').click(function ()
            {

                var judgeQuestionTitle = $('#judgeQuestionTitle').val();
                var para = {};
                para.judge_question_title = judgeQuestionTitle;
                table.reload('question_set1', {
                    url: 'judgeQuestionInfoManage.aspx'
                    , methods: "post"
                    , where: para
                    , page: {
                        curr: 1
                    }
                });
            });

            function updateUserInfo()
            {

                //获取表单所有值
                var from = $("#UpdateMutilQuestionForm")[0];
                var para = {};
                para["operate"] = "update";
                para["table"] = "tb_judge_questions";
                var correct_answer = "";
                debugger;
                for (var i = 0; i < from.length; i++)
                {
                    var key = from[i].name;
                    if (key == "correct_answer")
                    {
                        if (from[i].checked == true)
                            correct_answer =  from[i].title;
                    } else
                    {
                        para[key] = from[i].value;
                    }

                }
                if (correct_answer == "对")
                {

                    correct_answer = "1";
                }
                else
                {
                    correct_answer = "0";
                }
                para["correct_answer"] = correct_answer;
                delete para[""];
                
               
                $.ajax({
                    url: '../operate/operateQuestionInfo.aspx',
                    datatype: 'text',
                    data: para,
                    success: function (res)
                    {
                        
                        var result = Number(res);
                        if (result > 0)
                        {
                            layer.msg('更改成功');
                        }
                        else if (result = 0)
                        {
                            layer.msg('更改失败！');
                        }

                    },
                    error: function (xhr, textStatus, errorThrown)
                    {
                        alert("状态码：" + xhr.status + "状态:" + xhr.readyState + "错误信息:" + xhr.statusText + "请求状态：" + textStatus + errorThrown);
                        //当前状态,0-未初始化，1-正在载入，2-已经载入，3-数据进行交互，4-完成。
                    }
                });
            }
           
                function AddMutilQuestion()
                {
                    //保存按钮

                    //获取所有输入值
                    var items = $('#AddMutilQuestionForm')[0];
                    var para = {};
                    para["operate"] = "add";
                    para["table"] = "tb_judge_questions";

                    para["judge_question_title"] = items[0].value;
                    para["course_id"] = items[3].value;

                    var b = $("input[type='radio'][name='correct_answer_1']:checked")[0].title
                    if (b == "对")
                    {
                        b = "1";
                    }
                    else
                    {
                        b = "0";
                    }
                    para["correct_answer"] = b;

                    
                    //上传到后台
                    $.ajax({
                        url: '../operate/operateQuestionInfo.aspx',
                        datatype: 'text',
                        methed: 'post',
                        data: para,
                        success: function (res)
                        {
                            if (res > 0)
                                alert("添加成功");
                            else if (res < 0)
                            {
                                alert("存在该题目");
                            } else
                                alert("添加失败");
                        }
                    });
                }
     
            
        });

        


    </script>

</body>

</html>
